import 'dart:io';

import 'package:common/common.dart';
import 'package:common/net/app_api.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mobile_app/app.dart';

Future<void> main() async {
  // 时间记录
  assert(() {
    LogUtils.v("run main Start:${DateTime.now()}");
    return true;
  }());
  // 预先初始化WidgetsFlutterBinding
  WidgetsFlutterBinding.ensureInitialized();

  initUiConfig();

  // 网络配置
  initHttpConfig();

  // 时间记录
  assert(() {
    LogUtils.v("run App Start:${DateTime.now()}");
    return true;
  }());
  await ScreenUtil.ensureScreenSize();
  // 加载引用
  runApp(const ProviderScope(child: Program()));
  // 微信注册
  if (Platform.isAndroid || Platform.isIOS) {
    Fluwx().registerApi(appId: AppConstants.weChatAppId, universalLink: AppConstants.appUniversalLink);
  }
}

/// UI基本配置
void initUiConfig() {
  // 状态栏透明，状态栏样式，状态栏图标样式处理
  SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle.light
      .copyWith(statusBarColor: Colors.transparent, statusBarBrightness: Brightness.light, statusBarIconBrightness: Brightness.dark);
  SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}

/// 网络配置
void initHttpConfig() {
  String baseUrl;
  if (kReleaseMode) {
    baseUrl = AppApi.productUrl;
  } else {
    baseUrl = AppApi.testUrl;
  }
  assert(() {
    baseUrl = AppApi.productUrl;
    return true;
  }());
  HttpManager().init(baseUrl, formatError: (tag, code, msg) {
    //TODO 统一的网络错误拦截错误处理
    LogUtils.e("$tag 请求异常，错误码:$code，错误信息:$msg");
    return Future.value(false);
  },
      interceptors: [
        //TODO 拦截器
      ],
      // 网络请求成功码
      successCode: AppConstants.httpSuccessCode);

  // 获取包名
  PackageInfo.fromPlatform().then((packageInfo) {
    HttpManager().client.interceptors.add(LogInterceptor(
        requestBody: true, responseBody: true, logPrint: (obj) => LogUtils.v(obj, tag: packageInfo.appName, showLine: false)));
  });
}
